package gdr.dev;

import java.awt.*;
import gdr.battle.*;
import gdr.graphics.*;
import gdr.map.*;
import gdr.player.*;
import gdr.menu.battle.*;
import gdr.action.*;
import gdr.action.azioniUtente.*;
import gdr.action.azioniEsecuzione.*;

/**
 * Genera casualmente uno scontro
 * @author Giorgio Vitale
 *
 */
public class RandomGenerator
{
	public Classe generateClasse(String name, BattleManagerOld manager){
		Classe c = new Classe();
		c.setName(name);
		
		Azione a = new Move();
		UnlockCondition uc = new UnlockConditionDefault();
		a.setNome("Move");
		uc.setClasse(c);
		a.setIsUnlocked(uc);
		a.setManager(manager);
		c.getAzioni().add(a);
		
		a = new Azione();
		a.setNome("Attack");
		uc = new UnlockConditionDefault();
		uc.setClasse(c);
		a.setIsUnlocked(uc);
		a.setManager(manager);
		c.getAzioni().add(a);
		
		a = new Azione();
		a.setNome("Suicide");
		uc = new UnlockConditionDefault();
		uc.setClasse(c);
		a.setIsUnlocked(uc);
		a.setManager(manager);
		c.getAzioni().add(a);
		
		return c;
	}
	/**
	 * costruisce un personaggio casuale
	 * @param name
	 * @return
	 */
	public DefaultPlayer generateDefaultPlayer(String name){
		DefaultPlayer p = new DefaultPlayer();
		p.setName(name);
		p.setLevel(rand(15,100));
		p.setExpNextLevel(p.expToNextLevel(p.getLevel()));
		Statistiche s = new Statistiche();
		for (int i=0; i<Statistiche.STAT_NUM; i++)
			s.setStat(i, rand(20,30));
		p.setBaseStat(s);
		
		s = new Statistiche();
		for (int i=0; i<Statistiche.STAT_NUM; i++)
			s.setStat(i, rand(8,15)*10);
		p.setIncrementStat(s);
		
		s = new Statistiche();
		for (int i=0; i<Statistiche.STAT_NUM; i++)
			s.setStat(i, p.getMaxStat(i));
		//s.setStat(Statistiche.HP, rand(p.getMaxStat(Statistiche.HP)));
		s.setStat(Statistiche.MP, rand(p.getMaxStat(Statistiche.MP)));
		p.setActualStat(s);
		
		return p;
	}
	
	public BattleMapUnit generateBattleMapUnit(BattleMapUnitGraphics r, CoordinateCalculator coordinateCalc, Position pos){
		BattleMapUnit u = new BattleMapUnit();
		if (r instanceof BattleMapUnitGraphicsEmpty)
			u = new BattleMapUnitEmpty();
		u.setPos(pos);
		r.setColor(Color.green);
		r.setMapUnit(u);
		r.setCoordinateCalc(coordinateCalc);
		u.setGraphicsUnit(r);
		return u;
	}
	
	public BattleMap generateBattleMap(CoordinateCalculator coordinateCalc){
		BattleMap m = new BattleMap(15,10);
		
		m.addBattleMapUnit(generateBattleMapUnit(new BattleMapUnitGraphics(),coordinateCalc, new Position(0, 0, 1)));
		m.addBattleMapUnit(generateBattleMapUnit(new BattleMapUnitGraphics(),coordinateCalc, new Position(m.getWidth()-1, 0, 1)));
		m.addBattleMapUnit(generateBattleMapUnit(new BattleMapUnitGraphics(),coordinateCalc, new Position(m.getWidth()-1,m.getHeight()-1, 1)));
		m.addBattleMapUnit(generateBattleMapUnit(new BattleMapUnitGraphics(),coordinateCalc, new Position(0, m.getHeight()-1, 1)));
		for (int i=0; i<m.getWidth(); i++)
			for (int j=0; j<m.getHeight(); j++)
				if (i+j<10 || i+j>13)
					m.addBattleMapUnit(generateBattleMapUnit(new BattleMapUnitGraphics(),coordinateCalc, new Position(i, j, 1)));
				else
					m.addBattleMapUnit(generateBattleMapUnit(new BattleMapUnitGraphicsEmpty(),coordinateCalc, new Position(i, j, 1)));
		return m;
	}
	 
	
	public BattleGraphics generateBaseBattleGraphics(SuperGDR9000 game)
	{	BattleManagerOld manager = new BattleManagerOld(game);
		BattleGraphics battle = new BattleGraphics();
		manager.setGame(game);
		
		manager.setManagerAzioniUtente(new ManagerAzioniUtente());
		GiroLibero giroLibero= new GiroLibero();
		giroLibero.setManager(manager);
		manager.getManagerAzioniUtente().setGiroLibero(giroLibero);
		manager.getManagerAzioniUtente().getElements().addFirst(giroLibero);
		
		manager.setBattleGraphics(battle);
		battle.setManager(manager);
		manager.setCondizioneVincita(new WinnerCondition());
		manager.getCondizioneVincita().setManager(manager);
		manager.setCondizionePerdita(new LooserCondition());
		manager.getCondizionePerdita().setManager(manager);
		
		CoordinateCalculator coordinateCalc = new CoordinateCalculator();
		coordinateCalc.setGame(game);
		manager.setMap(generateBattleMap(coordinateCalc));
		manager.setCoordinateCalc(coordinateCalc);
		
		manager.setCursor(new BattleMapCursor());
		manager.getCursor().setCoordinateCalc(coordinateCalc);
		manager.getCursor().setCursorPosition(new Position(5,4,0));
		manager.getCursor().setManager(manager);
		
		manager.setGestoreTurni(new GestoreTurni());
		Team tp = new Team();
		tp.setUser(true);
		tp.setName("utente");
		Team tc = new Team();
		tc.setName("cpu");
		tc.setUser(false);
		tp.getEnemy().add(tc);
		tc.getEnemy().add(tp);
		
		manager.getGestoreTurni().getListaTurni().add(tp);
		manager.getGestoreTurni().getListaTurni().add(tc);

		manager.getTeams().addFirst(tp);
		manager.getTeams().addLast(tc);
		tp.getEnemy().add(tc);
		tc.getEnemy().add(tp);
		
		DefaultPlayer p = generateDefaultPlayer("gio");
		p.setTeam(tp);
		tp.getPlayers().add(p);
		PlayerGraphics rp = new PlayerGraphics();
		rp.setMapUnit(manager.getMap().getBattleMapUnit(new Position(1,2,1)));
		rp.getMapUnit().getPlayer().add(p);
		rp.setColor(Color.red);
		rp.setCoordinateCalc(coordinateCalc);
		rp.setPlayer(p);
		manager.getPlayers().add(rp);
		p.setClasse(generateClasse("mage",manager));
		
		p = generateDefaultPlayer("ale");
		p.setTeam(tp);
		tp.getPlayers().add(p);
		rp = new PlayerGraphics();
		rp.setMapUnit(manager.getMap().getBattleMapUnit(new Position(3,2,1)));
		rp.getMapUnit().getPlayer().add(p);
		rp.setColor(Color.yellow);
		rp.setCoordinateCalc(coordinateCalc);
		rp.setPlayer(p);
		manager.getPlayers().add(rp);
		p.setClasse(generateClasse("pippone",manager));
		
		p = generateDefaultPlayer("monster");
		p.setTeam(tc);
		tc.getPlayers().add(p);
		rp = new PlayerGraphics();
		rp.setMapUnit(manager.getMap().getBattleMapUnit(new Position(3,3,1)));
		rp.getMapUnit().getPlayer().add(p);
		rp.setColor(Color.gray);
		rp.setCoordinateCalc(coordinateCalc);
		rp.setPlayer(p);
		manager.getPlayers().add(rp);
		p.setClasse(generateClasse("umpalumpa",manager));
		
		BattleMenuManager menu = new BattleMenuManager();
		menu.setManager(manager);
		manager.setMenuManager(menu);
		
		BattleMainMenu bmm = new BattleMainMenu(manager);
		menu.getElements().add(bmm);
		menu.setMainMenu(bmm);
		bmm.setGdr(manager.getGame());
		
		BattleMenuShowStat mss = new BattleMenuShowStat(manager);
		mss.setPlayer(manager.getFocusedPlayer());
		menu.getElements().add(mss);
		menu.setShowStat(mss);
		
		BattleActionMenu am = new BattleActionMenu(manager);
		am.setGdr(manager.getGame());
		menu.getElements().add(am);
		menu.setActionMenu(am);
		
		return battle;
	}
	/**
	 * restituisce un intero nell'intervallo [start,end], estremi inclusi
	 * @param start
	 * @param end
	 * @return
	 */
	public static int rand(int start, int end)
	{	return (int)(Math.random()*(end-start+1))+start;
	}
	
	/**
	 * restituisce un intero nell'intervallo [0, size], estremi inclusi
	 * @param start
	 * @param end
	 * @return
	 */
	public static int rand(int size)
	{	return rand(0, size);
	}

}
